// JavaScript Document
var currentDestinationColumn;
var linkImage;
function uploadFile(destinationColumn){
	currentDestinationColumn = destinationColumn;
	window.open ("../jsp/xupload.jsp", 
				 "FileUploadWindow","width=" + winWidth + ",height=" + winHeight +", status=0,toolbar=0");

}

function uploadImage(){
	
	if (document.getElementById('NewFile').value.length == 0 &&
		document.getElementById('WebImage').value.length == 0) {
		alert( 'Selecciona un archivo en tu computadora o escribe una direccion de internet') ;
		return ;
	} else {
		// Verify what type of image it is
		if (document.getElementById('NewFile').value.length > 0){
			// Upload image
			document.getElementById('uploadProgressDiv').style.visibility = 'visible';
			document.xupload.submit();
		} else {
			// Web Image.
			webImage = new Image();
			webImage.src = document.getElementById('WebImage').value;
			linkImage = webImage;
			setTimeout(verifyImage, 2000);
		}
		
	}
}

function verifyImage(){
	// Image exists !
	if (linkImage.width){
		window.opener.changeFile(webImageName);
		window.close();	
	} else {
		alert('La direccion de la imagen que pusiste es incorrecta');
	}
}

function changeFile(fileName){
	document.getElementById("UploadSpan" + currentDestinationColumn).innerHTML = getMediaPreview(fileName) +
		'<br><a href="javascript:uploadFile(\'' + currentDestinationColumn + '\')"> Cambiar imagen ...</a>';
	
	document.getElementById("UploadField" + currentDestinationColumn).value = fileName;
	
}

function getMediaPreview(filename){
		var mediaPreview = '<a href="javascript:openMediaWindow(' + '\'' + filename + '\'' + ')">';
		var extension = filename.substring(filename.length - 4, filename.length);
		if (extension != null){
			if (extension.toUpperCase() == ".GIF" || extension.toUpperCase() == ".JPG"){
				
				var iWidth = document.getElementById("width" + currentDestinationColumn).value;
				var iHeight = document.getElementById("height" + currentDestinationColumn).value;
				
				
				mediaPreview = mediaPreview  + '<img src="' +  filename + '" border=0 width="' + iWidth
					+ '" height="' + iHeight + '" onLoad="resize(\'image' + currentDestinationColumn + '\', ' 
					+  iWidth + ')" id="image' + currentDestinationColumn + '">';
					
			} else if (extension.toUpperCase() == ".SWF" || extension.toUpperCase() ==".FLV"){
				mediaPreview = mediaPreview  + '<img src="../html/images/FlashMovie.gif" border=0>';
			}
		} else {
			return "?";
		}
		mediaPreview = mediaPreview + '</a>';
		return mediaPreview;
}

function openMediaWindow(filename){
	if (filename.indexOf('thumb') > 0){
		filename = filename.substring(0, filename.indexOf('thumb') - 1);
	}
	window.open (filename, 
				 "FileUploadWindow","width=600,height=400, status=0,toolbar=0,resizable=1");
}

function resize(which, max) {
	var elem = document.getElementById(which);
	if (elem == undefined || elem == null) return false;
	if (max == undefined) max = 100;
	if (elem.width > elem.height) {
	  if (elem.width> max) elem.width = max;
	} else {
	  if (elem.height> max) elem.height = max;
	}
}


